%%% running onsets 6676

ftimes=get_frame_times(aux_data(2,:));
ftindex=1:length(ftimes);
time=ftindex/40;
time(150001)=[];

figure,
hold on
plot(proj_meta(3).rd(2).velM_smoothed)
plot(r_onset,0,'.r')
set(gca,'ylim',[-0.15 0.15])
set(gca,'xlim',[0 150000])
box off

rind=[];
rind=proj_meta(3).rd(2).velM_smoothed>0.001;
aaa=diff(rind==1);
r_onset=find(diff(aaa>0));
r_onset(logical([0 diff(r_onset)<200]))=[];
r_onset(r_onset<win)=[];
r_onset(r_onset>size(proj_meta(3).rd(2).act,2)-win)=[];

win = 1000 %peri-stim window
r_act=struct

for knd=1:size(proj_meta(3).rd(2).act,1)
    for ind=1:length(r_onset)
      r_act.c(knd).PST(ind,:)=proj_meta(3).rd(2).act(knd,r_onset(ind)-win:r_onset(ind)+win);
    end
end

cmaps_run=hsv(size(r_act.c(1).PST,1));

for xnd = 1:133
    figure,
    hold on
    for mnd = 1:size(r_act.c(1).PST,1)
        plot(time(1:2*win+1),r_act.c(xnd).PST(mnd,:),'Color',cmaps_run(mnd,:))
    end
    plot([win/40 win/40],[0.5 4],'r','LineWidth',2)
    plot(time(1:2*win+1),mean(r_act.c(xnd).PST,1),'k','LineWidth',2)
    set(gca,'xlim',[0 2*win/40])
    box off
end

figure,
hold on
for and = 1:length(r_act.c)
    for mnd = 1:size(r_act.c(1).PST,1)
        plot(time(1:2*win+1),r_act.c(and).PST(mnd,:),'Color',[.8 .8 .8])
    end
    r_act_c_mean(and,:)=mean(r_act.c(and).PST(1:length(r_onset),:));
end
plot(time(1:2*win+1),mean(r_act_c_mean),'k','LineWidth',2)
plot([win/40 win/40],[0.5 6],'r','LineWidth',2)
set(gca,'xlim',[-0.1 2*win/40])
set(gca,'TickLength',[-0.005 0.025])
box off
set(gca,'ylim',[1 1.08])

win2av=1000

figure,
hold on
for and = 1:length(r_act.c)
    plot(1,mean(r_act_c_mean(and,1:win2av),2),'s')
    plot(1.1,mean(r_act_c_mean(and,win2av:2*win2av),2),'s')
    plot([1 1.1], [mean(r_act_c_mean(and,1:win2av),2) mean(r_act_c_mean(and,win2av:2*win2av),2)],':')
end
plot(1,mean(mean(r_act_c_mean(1:length(r_act.c),1:win2av))),'rs','LineWidth',2)
plot(1.1,mean(mean(r_act_c_mean(1:length(r_act.c),win2av:2*win2av))),'rs','LineWidth',2)
plot([1 1.1], [mean(mean(r_act_c_mean(1:length(r_act.c),1:win2av))) mean(mean(r_act_c_mean(1:length(r_act.c),win2av:2*win2av)))],'r')
set(gca, 'ylim', [0.98 1.18], 'xlim', [0.95 1.15])
box off